#include "xfsm.h"
// we will definitely initialize a local file system
#include "xlocal_fs.h"
#include "xzip_file_system.h"

#include "xsys_log.h"
#include "xatexit_manager.h"

namespace xge{

  // This is directly passing back the pointer
  // which enable the return value optimizing
  FileSystemPtr FileSystemManager::GetFileSystem( const String& fs_name ) {
    FileSystemPtrMap::iterator iret = file_ptr_.find( fs_name );
    if( iret == file_ptr_.end() ) {
      return FileSystemPtr();
    }
    return (iret->second);
  }
  // Register a archive into the fs_manager
  bool FileSystemManager::RegisterFileSystem( FileSystem* fs_ptr ) {
    std::pair<FileSystemPtrMap::iterator,bool> ret =
      file_ptr_.insert( FileSystemPtrMap::value_type( fs_ptr->name() , FileSystemPtr(fs_ptr) ) ) ;
    SYS_LOG(INFO,false,_xT("The file system: %s has been registered in the XFileSystemManager."),fs_ptr->name().c_str());
    return ret.second;
  }

  bool FileSystemManager::Initialize() {

    // Register the local file system 
    // This system MUST registered at very first !
    if( !xge::xge_internal::RegsiterLocalFileSystem() ) {
      SYS_LOG(FATAL,true,_xT("Can not register the local file system,this seems to be impossible!"));
      // impossible reach
      return false;
    }
    // Register the zip file system
    if( !xge::xge_internal::RegisterZipFileSystem() ) {
      SYS_LOG(FATAL,true,_xT("Can not register the zip file system,this seems to be impossible!"));
      return false;
    }
    // TODO:: Add other file system here
    SYS_LOG(INFO,true,_xT("File system initialize successfully!"));
    return true;
  }

}
